home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / cops / cops_104 / perl / suid.chk < prev    next >
Encoding:
Text File  |  1992-03-10  |  4.1 KB  |  143 lines

  1. #!/bin/sh -- need to mention perl here to avoid recursion
  2. 'true' || eval 'exec perl -S $0 $argv:q';
  3. eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
  4. & eval 'exec /usr/local/bin/perl -S $0 $argv:q'
  5.         if 0;
  6.  
  7. #
  8. #  Usage: suid.chk [-n] [-s secure_dir] [search_starting_directory]
  9. #
  10. #   Shell script intended to be run periodically by cron in order
  11. #   to spot changes in files with the suid or sgid bits set.
  12. #
  13. #    suid.chk    840919        Prentiss Riddle
  14. #
  15. #     This changes into the $SECURE directory first, then 
  16. #   uses find(1) to search the directories in $SEARCH for all
  17. #   files with the 4000 or 2000 permission bits set.  $STOP is a file
  18. #   containing "ls -gildsa" output for known setuid or setgid programs.
  19. #   Any additions or changes to this list represent potential security
  20. #   problems, so they are reported.
  21. #
  22. #  Modified 8/15/89, Dan Farmer:
  23. #    Just changed the program/doc names and some of the temp
  24. #  files to make it fit in with the rest of the programs....
  25. #  Modified 12/26/90, df
  26. #       Now flags SUID shell scripts and world writeable SUID files, too.
  27. #
  28. #  Rewritten in perl, 1/17/91, df
  29. #  Major hacks by tchrist 5/14/91
  30. #
  31.  
  32. require "hostname.pl";
  33. require "is_able.pl";
  34. require "file_owner.pl";
  35. require "pathconf.pl";
  36. require "chk_strings.pl";
  37. require "pass.cache.pl";
  38. package suid_chk; # name space protection
  39. $debug=0;
  40.  
  41. #
  42. # Getopts stuff
  43. $usage = "Usage: $0 [-n] [-s secure_dir] [starting_directory]\n";
  44. require 'getopts.pl';
  45. # Process the command args; Either specify verbose or an alternate config file:
  46. die $usage unless &'Getopts('ns:');
  47.  
  48. $suid_dir = $'SECURE || '.';
  49. if (defined($'opt_s)) { $suid_dir = $'opt_s; }
  50.  
  51. # Do NFS stuff?  Yes unless opt:
  52. if (defined($'opt_n)) { $skip_nfs = 1; }
  53. else { $skip_nfs = 0; }
  54.  
  55. $STOP="$suid_dir/suid.stop" unless defined $STOP;
  56. $FINDARGS="" unless defined $FINDARGS;
  57. $LSARGS=" -glids" unless defined $LSARGS;
  58.  
  59. $TEMPOLD="$suid_dir/fsold$$";
  60. $TEMPCUR="$suid_dir/fscur$$";
  61. $TEMPNEW="$suid_dir/fsnew$$";
  62. $TEMPGON="$suid_dir/fsgon$$";
  63. $TEMPM="$suid_dir/fsm$$";
  64.  
  65. if (@ARGV > 1) { die $usage; }
  66. elsif (@ARGV == 1) { $start_dir = shift; }
  67.  
  68. # these may be terribly rash assumptions....
  69. $start_dir="/" unless defined $start_dir;
  70. $find_can_ls = 1 unless defined $find_can_ls;
  71.  
  72. $NONFS = '-type d \( -fstype nfs -prune \) -o' if $skip_nfs;  
  73. $find_ls = $find_can_ls ? '-ls' : "-exec $'LS $LSARGS {} \\;";
  74.  
  75. die "Error -- Security directory $suid_dir doesn't exist\n"
  76.     unless -d $suid_dir;
  77. unless (-d $suid_dir) {
  78.     mkdir($suid_dir, 0700) || die "can't mkdir $suid_dir: $!";
  79. chdir $suid_dir || die "can't chdir $suid_dir: $!\n";
  80.  
  81. # find the setuid programs and sort
  82. &run("$'FIND $start_dir $FINDARGS $NONFS -type f \\( -perm -4000 -o -perm -2000 \\) $find_ls | $'SORT > $TEMPCUR");
  83.  
  84. # compare with the sorted stop list
  85. # create stop file if needed
  86. if (! -f $STOP) { open(S,">$STOP"); close(S); }
  87.  
  88. &run("$'SORT <$STOP >$TEMPOLD");
  89. &run("$'COMM -13 $TEMPOLD $TEMPCUR | $'SORT +8 >$TEMPNEW");
  90. &run("$'COMM -23 $TEMPOLD $TEMPCUR | $'SORT +8 >$TEMPGON");
  91.  
  92. local($is_able'silent) = 1;
  93. local($chk_strings'recurse) = 0 unless defined $chk_strings'recurse;
  94.  
  95. # report changes
  96. if (-s $TEMPNEW || -s $TEMPGON) {
  97.     if (-s $TEMPNEW) {
  98.     open TEMPNEW || die "Can't open $TEMPNEW: $!\n";
  99.     while (<TEMPNEW>) {
  100.         ($file) = /(\S+)$/;
  101.  
  102.         # don't want SUID files to be world writable!
  103.         # although *reasonable* systems clear the bit on write
  104.         print "Warning!  SUID file $file is _World_ writable!\n" 
  105.         if &'is_able ($file, "w", "w"); 
  106.             
  107.         if (-r $file && -f _ && -T $file) {
  108.         print "Warning!  ", &'Owner($file) ? '' : 'ROOT-owned ', 
  109.             "SUID file $file is a non-binary, executable file!\n";
  110.         }
  111.  
  112.         &'chk_strings($file) if -r _;
  113.     }
  114.     close TEMPNEW;
  115.     }
  116.  
  117.     if (-s $TEMPNEW) {
  118.     open TEMPNEW || die "Can't reopen $TEMPNEW: $!\n";
  119.     print "\nThese files are newly setuid/setgid:\n\n";
  120.     print while <TEMPNEW>;
  121.     }
  122.  
  123.     if (-s $TEMPGON) {
  124.     open TEMPGON || die "Can't reopen $TEMPGON: $!\n";
  125.     print "\nThese files are no longer setuid/setgid:\n\n";
  126.     print while <TEMPGON>;
  127.     }
  128.  
  129. }
  130.  
  131. unlink $TEMPOLD, $TEMPCUR, $TEMPNEW, $TEMPGON;
  132.  
  133. sub run {
  134.     print "running: $_[0]\n" if $debug;
  135.     system $_[0];
  136.     warn "command $_[0] returned $?" if $?;
  137.  
  138. #  end it all....
  139.  
  140. 1;
  141.